$(function () {
    allSportMarkTable();
    getGradeList();
    getSprotProjectList();
})


/*获取年级下拉数据*/
function getGradeList() {
    $.DemoPost("/grade/getGradePageByUserInfo?token="+sessionStorage.getItem("token"),
        {"pageNum":1,"pageSize":100},
        function (data) {
            if(data.success)
            {
                $.each(data.obj.list,function (i,d) {
                    $("#gradeList").append('<option value="'+d.id+'">'+d.gradeName+'</option>');
                });
            }
            else{
                layer.msg(data.msg);
            }
        },function (e) {
            layer.msg("初始化数据失败");
        });
}

function getSprotProjectList() {
    $.DemoPost("/sportsProject/getSportsProjectPageByUserInfo?token="+sessionStorage.getItem("token"),
        {"pageNum":1,"pageSize":100},
        function (data) {
            if(data.success)
            {
                $.each(data.obj.list,function (i,d) {
                    $("#sprotProjectList").append('<option value="'+d.id+'">'+d.projectName+'</option>');
                });
            }
            else{
                layer.msg(data.msg);
            }
        },function (e) {
            layer.msg("初始化数据失败");
        });
}


/*获取体质项下拉数据*/
function getCorporeityList() {
    $.DemoGet("/dictionary/getCorporeitys",
        function (data) {
            if(data.success)
            {
                $("#corporeityList").html("<option value=''>请选择</option>");
                $.each(data.obj,function (i,d) {
                    $("#corporeityList").append("<option value='"+d.id+"'>"+d.disValue+"</option>");
                });
            }
            else{
                layer.msg(data.msg);
            }
        },function (e) {
            layer.msg("初始化数据失败");
        });
}


function gradeChang() {


    var gradeId = $("#gradeList").val();
    if(gradeId=='')
    {
        $("#classList").html( "<option value=''>请选择</option>");
        return;
    }
    $.DemoGet("/classInfo/getClassListByGradeId/"+gradeId,function (data) {
        if(data.success)
        {
            $("#classList").html( "<option value=''>请选择</option>");
            $.each(data.obj,function (i,d) {
                $("#classList").append(" <option value='"+d.id+"'>"+d.className+"</option>");
            });
        }
        else
        {
            layer.msg(data.msg);
        }
    },function (e) {
        layer.msg("操作失败");
    });
}




function query(params) {
    var studentName = $("#studentName").val();
    var gradeName = $("#gradeName").val();
    var param = {
        studentName:studentName,
        gradeName:gradeName,
        pageNum : params.offset / params.limit + 1,
        pageSize : params.limit
    };
    return param;
}

/*获取列表数据*/
function allSportMarkTable() {
    $("#sport_mark_list").bootstrapTable({
        url:$.ip+"/studentproject/getStudentProjectList?token="+sessionStorage.getItem("token"),
        method:'post',
        queryParams:query,
        sidePagination: "server",
        pagination: true,
        striped:true,
        pageNumber:1,
        pageSize: 5,
        pageList: [10],
        dataType:"json",
        contentType:"application/json",
        responseHandler:function (res) {
            return {
                total:res.obj.total,
                rows :res.obj.list
            }
        },
        columns: [
            {
                title: '序号',
                field: 'id',
                align: 'center',
                valign: 'middle',
                formatter:function (value,row,index){
                    return index+1;
                }
            },
            {
                title: '姓名',
                align: 'center',
                field: 'name',
                valign: 'middle',

            }  ,
            {
                title: '学号',
                field: 'studentCode',
                align: 'center',
                valign: 'middle'
            },
            {
                title: '班级',
                align: 'center',
                field: 'className',
                valign: 'middle',
                formatter:function (className,row) {
                    var gradeName = row.gradeName;
                    return gradeName+""+className;
                }
            },
            {
                title: '性别',
                align: 'center',
                field: 'sex',
                valign: 'middle',

            },
            {
                title: '体育成绩',
                align: 'center',
                field: 'studentProjectInfos',
                valign: 'middle',
                formatter:function (studentProjectInfos,row) {
                    var labText = "";
                    for(var i = 0 ;i<studentProjectInfos.length;i++)
                    {
                        labText = labText+studentProjectInfos[i]["project_name"]+"  "+studentProjectInfos[i]["mark"]+"  "+studentProjectInfos[i]["grade_name"]+"</br>";
                    }
                    return labText;
                }
            },
            {
                title: '平均分',
                align: 'center',
                field: 'sportsProjectVaule',
                valign: 'middle',

            } ,
            {
                title: '等级',
                align: 'center',
                field: 'sportsProjectGrade',
                valign: 'middle',

            },{
                title: 'BMI',
                align: 'center',
                field: 'bmiValue',
                valign: 'middle',

            },{
                title: 'BMR',
                align: 'center',
                field: 'bmrValue',
                valign: 'middle',

            } ,
            {
                title: '操作',
                align: 'center',
                field: 'id',
                width:120,
                valign: 'middle',
                formatter:function (id,row) {
                    return '<a onclick="update(this)" data-id="'+id+'"  class="btn btn-primary " href="javascript:;">详情</a>';
                }
            },
        ]
    });
}


function add(){
    popdiv("tiyu-info","添加",["50%","50%"],function (index,layero) {
        layer.close(index);
    })
}
function update(obj){
    var id = $(obj).data("id");
    $.DemoGet("/studentproject/getStudentProjectById/"+id,function (data) {
        if(data.success)
        {
            var source = data.obj;
            $("#name").text(source.name);
            $("#studentCode").text(source.studentCode);
            $("#className").text(source.gradeName+""+source.className);
            $("#sex").text(source.sex);
            $("#sportsProjectVaule").text(source.sportsProjectVaule);
            $("#bmrVal").text(source.bmrValue);
            $("#bmiVal").text(source.bmiValue);
            $("#sportsProjectGrade").text(source.sportsProjectGrade);
            $("#sportMarkList").html();
            var sportMarkHtml="";
            for(var i=0;i<source.studentProjectInfos.length;i++)
            {
                sportMarkHtml=sportMarkHtml+"<div class=\"col-md-4  text-right\">"+source.studentProjectInfos[i]['project_name']+"</div><div class=\"col-md-2\">"+source.studentProjectInfos[i]['mark']+"</div><div class=\"col-md-1\">   "+source.studentProjectInfos[i]['grade_val']+"</div><div class=\"col-md-2\">  "+source.studentProjectInfos[i]['grade_name']+"</div><div class=\"col-md-2  text-left\"><a onclick=\"historyshow('"+source.studentProjectInfos[i]['student_id']+"','"+source.studentProjectInfos[i]['project_id']+"')\" class=\"text-primary\" href=\"javascript:;\">历史记录</a></div></div>";
            }
            $("#sportMarkList").html(sportMarkHtml);
        }
        else {
            layer.msg(data.msg);
        }
    },function (e) {
        layer.msg("操作失败");
    });
    popdiv("tiyu-cha","详情",["50%","80%"],updateStudentProject)
}

function updateStudentProject() {

}

function exprot() {
    tan("导出可能会使用部分时间，是否确认导出？",function () {
        var studentName = $("#studentName").val();
        var gradeName = $("#gradeName").val();
        location.href = $.ip+"/studentproject/export?token="+sessionStorage.getItem("token")+"&gradeName="+gradeName+"&studentName="+studentName
    });

}
function historyshow(studentId,projectId) {
    $.DemoPost("/studentproject/getStudentProjectByStudentId",
        {"studentId":studentId,"projectId":projectId},
        function (data) {
            if(data.success)
            {
                var historyHtml = "";
                var index = 0;
                for(var i=0;i<data.obj.length;i++)
                {
                    index++;
                    historyHtml=historyHtml+"<tr><td>"+index+"</td><td>"+data.obj[i].testTime+"</td><td>"+data.obj[i].mark+"</td><td>"+data.obj[i].gradeVal+"</td></tr>";
                }
                $("#historyList").html(historyHtml);
            }
            else {
                layer.msg(data.msg);
            }

        },function (e) {
            layer.msg("操作失败");
        });
    popdiv("tiyu-history","历史记录",["50%","50%"],function (index,layero) {
        layer.close(index);
    })
}
function out(){
    $("#gradeList").val("");
    $("#classList").val("");
    $("#sprotProjectList").val("");
    $("#file").val("");
    popdiv("export","导入",["50%","80%"],function (index,layero) {
        var classId = $("#classList").val();
        var projectId = $("#sprotProjectList").val();
        var file = $("#file").val();
        if(classId==''){
            layer.msg("请选择班级");
        }
        else if(projectId=='')
        {
            layer.msg("请选择体育项目");
        }
        else if(file=='')
        {
            layer.msg("请选择Excel文件");
        }
        else {
            $.ajaxFileUpload({
                url:$.ip+'/studentproject/uploadStudentProject', //用于文件上传的服务器端请求地址
                type: 'post',
                data: {projectId:projectId,classId:classId}, //此参数非常严谨，写错一个引号都不行
                secureuri: false, //一般设置为false
                fileElementId: 'file', //文件上传空间的id属性  <input type="file" id="file" name="file" />
                dataType: 'json', //返回值类型 一般设置为json
                success: function (data, status)  //服务器成功响应处理函数
                {
                    layer.msg("上传成功");
                    layer.close(index);
                },
                error: function (data, status, e)//服务器响应失败处理函数
                {
                    layer.msg("上传失败");
                }
            })
        }
        return false;
    })
}
getTime("time","datetime");

$("#btnQuery").click(function () {
    $("#sport_mark_list").bootstrapTable('refresh');
});